package com.zl.gmadmin.controller;

import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zl.gmadmin.common.ApiResponse;
import com.zl.gmadmin.common.Status;
import com.zl.gmadmin.entity.SysDept;
import com.zl.gmadmin.service.SysDeptService;
import com.zl.gmadmin.utils.LogUtil;
import com.zl.gmadmin.payload.DeptQueryCriteria;
import com.zl.gmadmin.vo.dept.DeptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2021/2/20 17:06
 */
@Api(tags = "系统:部门管理")
@RestController
@RequestMapping("/api/unit/dept")
public class SysDeptController {

    private final SysDeptService sysDeptService;

    public SysDeptController(SysDeptService sysDeptService) {
        this.sysDeptService = sysDeptService;
    }

    @GetMapping(value = "query")
    @ApiOperation("查询部门:根据条件查询部门")
    public ApiResponse queryDepts(DeptQueryCriteria deptQueryCriteria) {
//        LogUtil.debug("接收的数据 :"+JSONUtil.toJsonStr(deptQueryCriteria));
//        LogUtil.debug("返回的数据:"+ JSONUtil.toJsonStr(sysDeptService.queryDepts(deptQueryCriteria)));

        return ApiResponse.ofStatus(Status.SUCCESS, sysDeptService.queryDepts(deptQueryCriteria));
    }

    @PostMapping("/supDepts")
    @ApiOperation("查询部门:根据ID获取同级与上级数据")
    public ApiResponse supDepts(@RequestBody List<Long> deptIds) {
        LogUtil.debug("查询编辑操作" + deptIds);
        List<DeptVo> deptVos = sysDeptService.supDepts(deptIds);


        return ApiResponse.ofSuccess(deptVos);
    }


    @ApiOperation(value = "分页获取部门列表")
    @GetMapping
    public ApiResponse selectPageVo(DeptQueryCriteria criteria, Page<SysDept> page) {

        return ApiResponse.ofSuccess(sysDeptService.selectPageVo(criteria, page));
    }

    @ApiOperation("新增部门")
    @PostMapping
    public ApiResponse addDept(@RequestBody SysDept sysDept) {
        LogUtil.debug("新增");
        LogUtil.debug(JSONUtil.toJsonStr(sysDept));
        sysDeptService.saveDept(sysDept);
        return ApiResponse.ofSuccess();
    }

    @ApiOperation("更新部门")
    @PutMapping
    public ApiResponse updateDept(@RequestBody SysDept sysDept) {
        LogUtil.debug("更新");
        LogUtil.debug(JSONUtil.toJsonStr(sysDept));
        sysDeptService.updateDept(sysDept);
        return ApiResponse.ofSuccess();
    }

    @ApiOperation("删除部门")
    @DeleteMapping
    public ApiResponse delDept(@RequestBody List<Long> ids) {
        sysDeptService.removeByIdsAndVerification(ids);
        return ApiResponse.ofSuccess();
    }


    @ApiOperation(value = "获取当前部门的同级最大排序号")
    @GetMapping("sameLevelDeptMaxSort")
    public ApiResponse sameLevelDeptMaxSort(DeptQueryCriteria queryCriteria) {

//        LogUtil.debug("获取当前级别最大的排序+1 :" +queryCriteria.getPid());
        Optional<Long> longOptional= sysDeptService.getSameLevelDeptMaxSort(queryCriteria.getPid());
       long max=1l;
        if (longOptional.isPresent()){
            max=longOptional.get();
        }
        return ApiResponse.ofSuccess(max);
    }

}
